//
//  DonationVC.m
//  SelfDriving
//
//  Created by apple on 16/4/19.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "DonationVC.h"
#import "BusinessService+User.h"
@interface DonationVC ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITextField *moneryTextField;
@property (weak, nonatomic) IBOutlet UITextField *blessingTextField;
@property (weak, nonatomic) IBOutlet UILabel *monery;
@property (weak, nonatomic) IBOutlet UIImageView *headerImg;
@property (weak, nonatomic) IBOutlet UILabel *nameLbl;

@end

@implementation DonationVC

- (void)viewDidLoad {
    [super viewDidLoad];


    [[AppDelegate shareDelegate]refuser:[AppDelegate shareDelegate].user.uid success:^(id as) {
        LoginResponseModel *user = as;
        _monery.text = [NSString stringWithFormat:@"当前余额:￥%@",user.money];
    }];
    
   
    if ([self respondsToSelector:@selector(edgesForExtendedLayout)])
    {
        self.edgesForExtendedLayout = UIRectEdgeNone;
    }
    
    //xhw2018-3-15加
    [self xhwFoo];
    
    //xhw2018-1-25加:红包金额只能输入数字和一位小数点
    _moneryTextField.delegate = self;
    
    
}

- (void)viewWillAppear:(BOOL)animated {
    
}

- (void)xhwFoo {
    __weak typeof (self)weakself = self;
    
    [_headerImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakself .view).with.offset(20);
        make.centerX.equalTo(weakself .view);
        make.size.mas_equalTo(CGSizeMake(80, 80));
    }];
    NSLog(@"头像str:%@",_head);
    [_headerImg sd_setImageWithURL:[NSURL URLWithString:_head] placeholderImage:[UIImage imageNamed:@"notig"]];
    _headerImg.layer.masksToBounds = YES;
    NSLog(@"%f",_headerImg.frame.size.width/2);
    _headerImg.layer.cornerRadius = _headerImg.frame.size.width/2;
    _nameLbl.font = [UIFont systemFontOfSize:14];
    _nameLbl.text = _name;
    
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}




- (IBAction)submitAction:(UIButton *)sender {
    if([_moneryTextField.text trim].length == 0){
        [SVProgressHUD showErrorWithStatus:@"请输入金额"];
        return;
    }
    NSMutableDictionary *ddic = [[NSMutableDictionary alloc]init];
    NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
    [dic setObject:_uid forKey:@"fid"];
    [dic setObject:_uid forKey:@"fuid"];
    [dic setObject:[_moneryTextField.text trim] forKey:@"money"];
    [dic setObject:[_blessingTextField.text trim].length==0?@"恭喜发财，大吉大利！":[_blessingTextField.text trim] forKey:@"content"];
    [[BusinessService shareInstance]getUserDetail:dic success:^(id result) {
        LoginResponseModel *model = result;
        // {"content":"恭喜发财，大吉大利!","fid":"200019","fnikename":"","money":"0.1","nickname":"nye"}
        [ddic setObject:[AppDelegate shareDelegate].user.nickname forKey:@"nickname"];
        [ddic setObject:_uid forKey:@"fid"];
        [ddic setObject:[_moneryTextField.text trim] forKey:@"money"];
        [ddic setObject:[_blessingTextField.text trim].length==0?@"恭喜发财，大吉大利！":[_blessingTextField.text trim] forKey:@"content"];
        [ddic setObject:model.nickname forKey:@"fnikename"];
        [[BusinessService shareInstance]giveMoney:dic success:^(id result) {
            [_delegate didSelectZhuanZ:ddic];
            [self.navigationController popViewControllerAnimated:YES];
        } failure:^(id result) {
            [SVProgressHUD showErrorWithStatus:result];
        }];
        
        
        
        
    } failure:^(id result) {
        
    }];
    
    
    
    
}
- (IBAction)backAction:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if (textField.text.length > 5) {
        return range.location < 6;
    }
    else if (textField.text.length == 5) {
        
        NSString *newStr = [textField.text stringByAppendingString:string];
        if ([newStr floatValue] > 100000) {
            [textField.text stringByReplacingCharactersInRange:range withString:@""];
            return NO;
        }
        else {
            BOOL isHaveDian = YES;
            if ([textField.text rangeOfString:@"."].location==NSNotFound) {
                isHaveDian=NO;
            }
            if ([string length]>0) {
                unichar single=[string characterAtIndex:0];//当前输入的字符
                if (single=='.')
                {
                    if(!isHaveDian)//text中还没有小数点
                    {
                        isHaveDian=YES;
                        return YES;
                    }else
                    {
                        [textField.text stringByReplacingCharactersInRange:range withString:@""];
                        return NO;
                    }
                }
                else
                {
                    if (isHaveDian)//存在小数点
                    {
                        //判断小数点的位数
                        NSRange ran=[textField.text rangeOfString:@"."];
                        NSInteger tt=range.location-ran.location;
                        if (tt <= 2){
                            return YES;
                        }else{
                            return NO;
                        }
                    }
                    else
                    {
                        return YES;
                    }
                }
            }
            else
            {
                return YES;
            }
        }
    }
    else{
        BOOL isHaveDian = YES;
        if ([textField.text rangeOfString:@"."].location==NSNotFound) {
            isHaveDian=NO;
        }
        if ([string length]>0)
        {
            unichar single=[string characterAtIndex:0];//当前输入的字符
            
            if ((single >='0' && single<='9') || single=='.')//数据格式正确
            {
                //首字母不能为小数点
                if([textField.text length]==0){
                    if(single == '.'){
                        [textField.text stringByReplacingCharactersInRange:range withString:@""];
                        return NO;
                        
                    }
                }
                if([textField.text length]==1 && [textField.text isEqualToString:@"0"]){
                    if(single != '.'){
                        [textField.text stringByReplacingCharactersInRange:range withString:@""];
                        return NO;
                        
                    }
                }
                if (single=='.')
                {
                    if(!isHaveDian)//text中还没有小数点
                    {
                        isHaveDian=YES;
                        return YES;
                    }else
                    {
                        [textField.text stringByReplacingCharactersInRange:range withString:@""];
                        return NO;
                    }
                }
                else
                {
                    if (isHaveDian)//存在小数点
                    {
                        //判断小数点的位数
                        NSRange ran=[textField.text rangeOfString:@"."];
                        NSInteger tt=range.location-ran.location;
                        if (tt <= 2){
                            return YES;
                        }else{
                            return NO;
                        }
                    }
                    else
                    {
                        return YES;
                    }
                }
            }else{//输入的数据格式不正确
                [textField.text stringByReplacingCharactersInRange:range withString:@""];
                return NO;
            }
        }
        else
        {
            return YES;
        }
    }
}


@end
